<?php
// error_reporting(E_ALL);
// ini_set("display_errors","On");

//就近 www.jiujin.me
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$id = $_GPC['id'];
//$qid=$_GPC['qid'];
//echo $_GPC['qid'];
$qid = !empty($_GPC['qid']) ? $_GPC['qid'] : '1';

//echo($_GPC['qid']);
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
//gmm 2015-12-16 扫码领余额
$time = time();
$saoyue = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_saoyue') . " WHERE id = '{$id}' and uniacid = '{$_W['uniacid']}' and status = 1 and 
	'{$time}' > starttime and '{$time}' < endtime ");
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4d6a0644cae71c05&secret=e57902975056e739bae60ae12c6c1419";
$access_msg = json_decode(file_get_contents($access_token));
$token = $access_msg->access_token;
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$_W[openid]";
$subscribe = json_decode(file_get_contents($subscribe_msg));
$gzxx = $subscribe->subscribe;
    $userinfo= pdo_fetch("SELECT * FROM " . tablename('ewei_shop_member') . " WHERE uid = '{$member['uid']}' and uniacid = '{$_W['uniacid']}'");
    if(empty($userinfo['realname']) || empty($userinfo['mobile']) || empty($userinfo['weixin']) || empty($userinfo['mobile']) || empty($userinfo['province']) || empty($userinfo['city'])){
         if($gzxx === 1){
         $url="http://wx.lanxuevip.com/app/index.php?i=5&c=entry&p=info&do=member&saoma=true&m=ewei_shop&con=".$_GPC['do']."&id=".$_GPC['id']."&qid=".$_GPC['qid'];
         header("Location: $url");
         exit;
         }
    }
if(!empty($saoyue)){
	$saoyue_limit = intval($saoyue['limit']);
	$saoyue_perlimit = intval($saoyue['perlimit']);
	//扫过的次数
	$all_count = pdo_fetch("SELECT count(*) as count FROM " . tablename('ewei_shop_saoyue_record') . " WHERE saoyue = '{$id}' and uniacid = '{$_W['uniacid']}'");
	if(intval($all_count['count']) < $saoyue_limit){
		$count = pdo_fetch("SELECT count(*) as count FROM " . tablename('ewei_shop_saoyue_record') . " WHERE saoyue = '{$id}' and uniacid = '{$_W['uniacid']}' and  uid = '{$member['uid']}'");
		if(intval($count['count']) < $saoyue_perlimit || $saoyue_perlimit == 0 || $saoyue_perlimit == null){		
  			$pid = pdo_fetch("SELECT id,qid FROM " . tablename('ewei_shop_saoyue_record') . " WHERE qid='{$qid}' and saoyue = '{$id}' and uniacid = '{$_W['uniacid']}' and  uid = '{$member['uid']}'");
                        $pid1 = pdo_fetch("SELECT id,qid FROM " . tablename('ewei_shop_saoyue_record') . " WHERE qid='{$qid}' and saoyue = '{$id}' and uniacid = '{$_W['uniacid']}'");
                                //echo("SELECT id,qid FROM " . tablename('ewei_shop_saoyue_record') . " WHERE qid='{$qid}' and saoyue = '{$id}' and uniacid = '{$_W['uniacid']}' and  uid = '{$member['uid']}'");
  				//if(intval($pid['qid'])==0){
  			if(empty($pid) && $saoyue['createtype']== 2 && $gzxx === 1){
			$saoyue_ok = 1;
			$saoyue_rest = $saoyue_limit - intval($count['count']) - 1;
			$insert = array(
				'uniacid' => $_W['uniacid'],
				'saoyue' => $id,
				'num' => $saoyue['num'],
				'createtime' => time(),
				'uid' => $member['uid'],
				'nickname' => $member['nickname'],
				'qid' => $qid
			);
			pdo_insert('ewei_shop_saoyue_record', $insert);
			//修改余额
			$credit2 = intval($member['credit2']) + intval($saoyue['num']);
			$data = array('credit2' => $credit2);
			pdo_update('mc_members', $data, array('uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
			$logno = m('common')->createNO('member_log', 'logno', 'RC');
			$charge = array('openid' => $member['openid'], 'uniacid' => $_W['uniacid'], 'logno' => $logno, 'type' => '0', 'createtime' => TIMESTAMP, 'status' => '1', 'title' => '扫码领余额', 'money' => $saoyue['num'], 'rechargetype' => 'saoyue');
			pdo_insert('ewei_shop_member_log', $charge);
			//pdo_update('ewei_shop_saoyue_record', array('qid' => $qid), array('id' => $pid['id'],'uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
                        }elseif(empty($pid1) && $saoyue['createtype']== 1 && $gzxx === 1){
                        $saoyue_ok = 1;
			$saoyue_rest = $saoyue_limit - intval($count['count']) - 1;
			$insert = array(
				'uniacid' => $_W['uniacid'],
				'saoyue' => $id,
				'num' => $saoyue['num'],
				'createtime' => time(),
				'uid' => $member['uid'],
				'nickname' => $member['nickname'],
				'qid' => $qid
			);
			pdo_insert('ewei_shop_saoyue_record', $insert);
			//修改余额
			$credit2 = intval($member['credit2']) + intval($saoyue['num']);
			$data = array('credit2' => $credit2);
			pdo_update('mc_members', $data, array('uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
			$logno = m('common')->createNO('member_log', 'logno', 'RC');
			$charge = array('openid' => $member['openid'], 'uniacid' => $_W['uniacid'], 'logno' => $logno, 'type' => '0', 'createtime' => TIMESTAMP, 'status' => '1', 'title' => '扫码领余额', 'money' => $saoyue['num'], 'rechargetype' => 'saoyue');
			pdo_insert('ewei_shop_member_log', $charge);
                        }else{
                             $saoyue_ok = 0;
  			}
			//}
		}else{
			$saoyue_ok = 0;
		}
	}else{
		$saoyue_ok = 0;
	}
}else{
	$saoyue_ok = 0;
}
include $this->template('saoyue/list');

